package net.daoke.relation.advice;

import net.daoke.relation.enums.ReturnCode;
import net.daoke.relation.exception.DefinitionException;
import net.daoke.relation.model.ResultData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     *
     */
    @ExceptionHandler(value = DefinitionException.class)
    @ResponseBody
    public ResultData bizExceptionHandler(DefinitionException e) {
        e.printStackTrace();
        return ResultData.fail(e.getErrorCode(), e.getErrorMsg());
    }

    /**
     * 处理其他异常
     *
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultData exceptionHandler(Exception e) {
        e.printStackTrace();
        return ResultData.fail(ReturnCode.RC500.getCode(), e.getMessage());
    }
}
